package com.tiennt.android.callnotes.db;

import java.util.ArrayList;

import com.tiennt.android.callnotes.models.contentholders.ListNotesItem;
import com.tiennt.android.callnotes.models.contentholders.SingleNote;
import com.tiennt.android.callnotes.utils.ProjectUtils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DbAdapter {
	private final String TAG = "DbAdapter";
	private final boolean DBG = true;

	private Context mContext;
	DbHandler mDatabaseHandler;
	SQLiteDatabase mSQLiteDatabase;

	public DbAdapter(Context context) {
		mContext = context;
	}

	public void open() {
		mDatabaseHandler = new DbHandler(mContext);
		mSQLiteDatabase = mDatabaseHandler.getWritableDatabase();
	}

	public void close() {
		mSQLiteDatabase.close();
		mDatabaseHandler.close();
	}

	public ArrayList<ListNotesItem> getAllNotes() {
		ArrayList<ListNotesItem> result = new ArrayList<ListNotesItem>();
		Cursor c = mSQLiteDatabase.query(DbUtils.TB_NOTESDATA_NAME, null, null,
				null, null, null, null);
		if (c != null) {
			if (c.moveToFirst()) {
				do {
					ListNotesItem item = new ListNotesItem();

					item.setId(c.getInt(c
							.getColumnIndex(DbUtils.TB_NOTESDATA_KEY_ROWID)));
					log(item.getId() + "");

					item.setNoteTitle(c.getString(c
							.getColumnIndex(DbUtils.TB_NOTESDATA_KEY_TITLE)));

					item.setNoteContent(c.getString(c
							.getColumnIndex(DbUtils.TB_NOTESDATA_KEY_CONTENT)));

					item.setContactId(c.getString(c
							.getColumnIndex(DbUtils.TB_NOTESDATA_KEY_CONTACTID)));
					log("ID" + item.getContactId());

					item.setInStatus(c.getInt(c
							.getColumnIndex(DbUtils.TB_NOTESDATA_KEY_INCOMING)) == 1 ? true
							: false);

					item.setOutStatus(c.getInt(c
							.getColumnIndex(DbUtils.TB_NOTESDATA_KEY_OUTGOING)) == 1 ? true
							: false);
					result.add(item);
				} while (c.moveToNext());
			}
		}
		c.close();
		return result;
	}

	public ArrayList<SingleNote> getAllNotesByContact(String contactId) {
		ArrayList<SingleNote> result = new ArrayList<SingleNote>();
		Cursor c = mSQLiteDatabase.query(DbUtils.TB_NOTESDATA_NAME,
				new String[] { DbUtils.TB_NOTESDATA_KEY_ROWID,
						DbUtils.TB_NOTESDATA_KEY_TITLE,
						DbUtils.TB_NOTESDATA_KEY_CONTENT },
				DbUtils.TB_NOTESDATA_KEY_CONTACTID + " = " + contactId, null,
				null, null, null);
		if (c != null) {
			if (c.moveToFirst()) {
				do {
					SingleNote item = new SingleNote();

					item.setNoteId(c.getInt(c
							.getColumnIndex(DbUtils.TB_NOTESDATA_KEY_ROWID)));
					log(item.getId() + "");

					item.setNoteTitle(c.getString(c
							.getColumnIndex(DbUtils.TB_NOTESDATA_KEY_TITLE)));

					item.setNoteContent(c.getString(c
							.getColumnIndex(DbUtils.TB_NOTESDATA_KEY_CONTENT)));

					result.add(item);
				} while (c.moveToNext());
			}
		}
		c.close();
		return result;
	}

	public ArrayList<SingleNote> getNotesForPopup(String contactId, int type) {
		ArrayList<SingleNote> result = new ArrayList<SingleNote>();
		String typeConstrain = "";
		if (type == ProjectUtils.CALLTYPE_INCOMING) {
			typeConstrain = DbUtils.TB_NOTESDATA_KEY_INCOMING + " = 1 ";
		} else if (type == ProjectUtils.CALLTYPE_OUTGOING) {
			typeConstrain = DbUtils.TB_NOTESDATA_KEY_OUTGOING + " = 1 ";
		}

		Cursor c = mSQLiteDatabase.query(DbUtils.TB_NOTESDATA_NAME,
				new String[] { DbUtils.TB_NOTESDATA_KEY_ROWID,
						DbUtils.TB_NOTESDATA_KEY_TITLE,
						DbUtils.TB_NOTESDATA_KEY_CONTENT, },
				DbUtils.TB_NOTESDATA_KEY_CONTACTID + " = " + contactId
						+ " and " + typeConstrain, null, null, null, null);
		if (c != null) {
			if (c.moveToFirst()) {
				do {

					SingleNote item = new SingleNote();

					item.setNoteId(c.getInt(c
							.getColumnIndex(DbUtils.TB_NOTESDATA_KEY_ROWID)));
					log(item.getId() + "");

					item.setNoteTitle(c.getString(c
							.getColumnIndex(DbUtils.TB_NOTESDATA_KEY_TITLE)));

					item.setNoteContent(c.getString(c
							.getColumnIndex(DbUtils.TB_NOTESDATA_KEY_CONTENT)));

					result.add(item);
				} while (c.moveToNext());
			}
		}
		c.close();
		return result;
	}

	public long saveNote(String title, String content, String contactId,
			int incoming, int outgoing, int enable) {
		ContentValues values = new ContentValues();
		values.put(DbUtils.TB_NOTESDATA_KEY_TITLE, title);
		values.put(DbUtils.TB_NOTESDATA_KEY_CONTENT, content);
		values.put(DbUtils.TB_NOTESDATA_KEY_CONTACTID, contactId);
		values.put(DbUtils.TB_NOTESDATA_KEY_INCOMING, incoming);
		values.put(DbUtils.TB_NOTESDATA_KEY_OUTGOING, outgoing);

		return mSQLiteDatabase.insert(DbUtils.TB_NOTESDATA_NAME, null, values);
	}

	public void deleteNotes(int noteId) {
		mSQLiteDatabase.delete(DbUtils.TB_NOTESDATA_NAME,
				DbUtils.TB_NOTESDATA_KEY_ROWID + " = " + noteId, null);
	}

	public int saveEditedNote(int id, String title, String content,
			String contactId, int incoming, int outgoing) {
		ContentValues values = new ContentValues();
		values.put(DbUtils.TB_NOTESDATA_KEY_TITLE, title);
		values.put(DbUtils.TB_NOTESDATA_KEY_CONTENT, content);
		values.put(DbUtils.TB_NOTESDATA_KEY_CONTACTID, contactId);
		values.put(DbUtils.TB_NOTESDATA_KEY_INCOMING, incoming);
		values.put(DbUtils.TB_NOTESDATA_KEY_OUTGOING, outgoing);

		return mSQLiteDatabase.update(DbUtils.TB_NOTESDATA_NAME, values,
				DbUtils.TB_NOTESDATA_KEY_ROWID + " = " + id, null);
	}

	public void log(String msg) {
		if (DBG) {
			Log.d(TAG, msg);
		}
	}

	public int dismissMessage(int messageId, int callType) {
		ContentValues values = new ContentValues();
		if (callType == ProjectUtils.CALLTYPE_INCOMING) {
			values.put(DbUtils.TB_NOTESDATA_KEY_INCOMING, 0);
		} else if (callType == ProjectUtils.CALLTYPE_OUTGOING) {
			values.put(DbUtils.TB_NOTESDATA_KEY_OUTGOING, 0);
		}
		return mSQLiteDatabase.update(DbUtils.TB_NOTESDATA_NAME, values,
				DbUtils.TB_NOTESDATA_KEY_ROWID + " = " + messageId, null);
	}

	public int getCount(String contactId, int callType) {
		String callTypeColumn = (callType == ProjectUtils.CALLTYPE_INCOMING) ? DbUtils.TB_NOTESDATA_KEY_INCOMING
				: DbUtils.TB_NOTESDATA_KEY_OUTGOING;

		return (int) DatabaseUtils.queryNumEntries(mSQLiteDatabase,
				DbUtils.TB_NOTESDATA_NAME, DbUtils.TB_NOTESDATA_KEY_CONTACTID
						+ " = '" + contactId + "' and " + callTypeColumn
						+ " = 1");
	}
}
